package com.scanner.demo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ScrollView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ScrollView mScrollView;
    TextView mTvData;
    Switch mSwitcher;
    Button mBtnScanner;

    BarCodeReceiver mReceiver;
    StringBuilder mBuilder;

    Toast mToast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mScrollView = (ScrollView) findViewById(R.id.scrollView);
        mSwitcher = (Switch) findViewById(R.id.switcher);
        mTvData = (TextView) findViewById(R.id.tv_data);
        mBtnScanner = (Button) findViewById(R.id.btn_send);

        mSwitcher.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
                if (checked) {
                    register();
                } else {
                    unRegister();
                }
                initStartButton();
            }
        });

        mBtnScanner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                if (!isRegister()) {
//                    showToast("请先开启开关");
//                    return;
//                }
                openScanner();
            }
        });

        mBtnScanner.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                openScanner();
                return true;
            }
        });

        register();
        initStartButton();
    }

    private void register() {
        if (mReceiver == null) {
            mReceiver = new BarCodeReceiver();
            mReceiver.setReceiverListener(new BarCodeReceiver.IReceiverListener() {
                @Override
                public void onReceive(String data) {
                    if (TextUtils.isEmpty(data)) {
                        data = "无数据";
                    }
                    save(data);
                    mTvData.setText(mBuilder.toString());
                    scrollToBottom();
                    showToast(data);
                }
            });
            BarCodeReceiver.register(this, mReceiver);
        }
    }

    private void openScanner() {
        Intent intent = new Intent();
        intent.setAction("com.barcode.sendBroadcastScan");
        sendBroadcast(intent);
    }

    private void unRegister() {
        if (mReceiver != null) {
            BarCodeReceiver.unregister(this, mReceiver);
            mReceiver = null;
        }
    }

    private boolean isRegister() {
        return mReceiver != null;
    }


    private void save(String data) {
        if (mBuilder == null) {
            mBuilder = new StringBuilder();
        }
        mBuilder.append(data);
        mBuilder.append("\n");
    }

    private void initStartButton() {
        if (!isRegister()) {
            mBtnScanner.setTextColor(ContextCompat.getColor(this, android.R.color.darker_gray));
        } else {
            mBtnScanner.setTextColor(ContextCompat.getColor(this, android.R.color.white));
        }
    }

    private void scrollToBottom() {
        mScrollView.post(new Runnable() {
            @Override
            public void run() {
                mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });
    }

    private void showToast(String msg) {
//        if (mToast == null) {
//            mToast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
//        }
//        mToast.setText(msg);
//        mToast.show();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unRegister();
    }
}
